<?php
namespace app\admin\model;
use think\Db;
use app\mouth\common\Encapsulation;
class BusinessModel extends Encapsulation{
    /**
     * @param string $where
     * @return bool|\think\Paginator
     * @throws \think\exception\DbException
     */
    public function index($add){
        $where['ma_state'] = ['1','2','3','4'];
        if (empty($add)){
            $business = Db::name('store')->where($where)->order('ma_id desc')->paginate(10);
        } else {
            $business = Db::name('store')->where('ma_state',$add)->order('ma_id desc')->paginate(10);
        }
        if ($business){
            return $business;
        }
        return false;
    }

    public function deleteUser($uid){
        return $this->delete('wx_user',['id'=>$uid]);
    }

    /**
     * 添加店铺
     * @param $value
     * @param $translation
     * @return bool|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function storeAdd($value,$translation){
        $examination = $this->add($value,$translation);
        if ($examination == false){
            return "请检查内容";
        }
        $stay = Db::name('store')->where('ma_name',$examination['ma_name'])->find();
        if ($stay){
            return "店铺已存在";
        }
        $examination['ma_time']           = time();
        $success = Db::name('store')->insert($examination);
        if ($success){
            return true;
        }
        return "添加失败";
    }

    /** 查找商户/删除
     * @param $ma_id  用户 id
     */
    public function modifyChange($ma_id,$move=''){
        if ($move == 2){
            $delete_id = Db::name('store')->where('ma_id',$ma_id)->delete();
            if ($delete_id){
                return true;
            }
            return false;
        }
        $store_id = Db::name('store')->where('ma_id',$ma_id)->find();
        if ($store_id){
            return $store_id;
        }
        return false;
    }
    public function storeAmend($whole,$translation){
        $examination = $this->add($whole,$translation);
        if ($examination == false){
            return "请检查内容";
        }
        $stay = Db::name('store')->where('ma_name',$whole['ma_name'])->find();
        if ($stay){
            if ($stay['ma_id'] != $whole['ma_id']){
                return "店铺已存在";
            }
        }

        $examination['ma_up_time']           = time();
        $success = db('store')->where('ma_id',$whole['ma_id'])->update($examination);
        if ($success){
            return true;
        }
        return "更新失败";
    }

    /**
     * 验证
     * @param $add
     * @param $translation
     * @return bool
     */
    public function add($add,$translation){

        foreach ($add as $k=>$v){
            if (empty($v)){
                return false;
            }
        }
        if ($add['ma_score'] > 6 || preg_match("/^1[34578]{1}\d{9}$/",$add['ma_number']) != 1){
            return false;
        }
        $revert['ma_name']           = $add['ma_name'];
        $revert['ma_score']          = $add['ma_score'];
        if ($translation != null){
            $revert['ma_logo']       = $translation;
        } else {
            $revert['ma_logo']       = $add['ma_logo'];
        }

        $revert['ma_synopsis']       = $add['ma_synopsis'];
        $revert['ma_state']          = $add['ma_state'];
        $revert['ma_business_hours'] = $add['ma_business_hours'].'-'.$add['ma_business_hoursl'];
        $revert['ma_number']         = $add['ma_number'];

        $revert['ma_position']       = $add['ma_position'];
        return $revert;
    }

    /**个人信息
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function userIndex(){
        $user = Db::name('wx_user')->order('id desc')->paginate(10);
        return $user;
    }

}